package com.example.test_witns.adapter.addSearchResult;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.test_witns.R;
import com.example.test_witns.pojo.AddSearchResult;

import java.io.ByteArrayOutputStream;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class AddSearchResultAdapter extends RecyclerView.Adapter<AddSearchResultAdapter.AddSearchResultViewHolder>{
    private List<AddSearchResult> addSearchResults;

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener{
        void onItemClicked(byte[] userAvatar, String userId, String uuid, String username);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public AddSearchResultAdapter( List<AddSearchResult> addSearchResults){ this.addSearchResults = addSearchResults; }

    /**
     * @param parent   The ViewGroup into which the new View will be added after it is bound to
     *                 an adapter position.
     * @param viewType The view type of the new View.
     * @return
     */
    @NonNull
    @Override
    public AddSearchResultViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_search_result, parent, false);
        return new AddSearchResultAdapter.AddSearchResultViewHolder(view, onItemClickListener);
    }

    /**
     * @param holder   The ViewHolder which should be updated to represent the contents of the
     *                 item at the given position in the data set.
     * @param position The position of the item within the adapter's data set.
     */
    @Override
    public void onBindViewHolder(@NonNull AddSearchResultViewHolder holder, int position) {
        AddSearchResult addSearchResult = addSearchResults.get(position);
        holder.user_Or_Group_NameTextView.setText(addSearchResult.getUsername());
        holder.uuid_ValueTextView.setText(addSearchResult.getUuid());
        holder.userId = addSearchResult.getUserId();

        if (addSearchResult.getAvatar() != null) {
            Toast.makeText(holder.itemView.getContext(), "getAvatar非空", Toast.LENGTH_SHORT).show();
            Bitmap bitmap = null;
            bitmap = BitmapFactory.decodeByteArray(addSearchResult.getAvatar(), 0, addSearchResult.getAvatar().length);
        }else if(addSearchResult.getAvatarURL() != null){

            String avatarUrl = addSearchResult.getAvatarURL();

            Glide.with(holder.itemView.getContext())
                    .load(avatarUrl)
                    .into(holder.user_AvatarCircleImageView);
        }else{
            Toast.makeText(holder.itemView.getContext(), "加载失败", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * @return
     */
    @Override
    public int getItemCount() {
        return addSearchResults.size();
    }

    public static class AddSearchResultViewHolder extends RecyclerView.ViewHolder {
        public TextView user_Or_Group_NameTextView;
        public TextView uuid_ValueTextView;
        public CircleImageView user_AvatarCircleImageView;
        public String userId;
        public byte[] userAvatar;

        public AddSearchResultViewHolder(View itemView, final OnItemClickListener listener) {
            super(itemView);
            user_Or_Group_NameTextView = itemView.findViewById(R.id.search_result_name);
            uuid_ValueTextView = itemView.findViewById(R.id.uuid_value);
            user_AvatarCircleImageView = itemView.findViewById(R.id.search_result_avatar);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uuid_Value = uuid_ValueTextView.getText().toString();
                    String username = user_Or_Group_NameTextView.getText().toString();
                    int position = getBindingAdapterPosition();

                    Bitmap bitmap1 = null;
                    if (user_AvatarCircleImageView.getDrawable() instanceof BitmapDrawable){
                        bitmap1 = ((BitmapDrawable)user_AvatarCircleImageView.getDrawable()).getBitmap();
                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                        bitmap1.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
                        userAvatar = outputStream.toByteArray();
                    }
                    listener.onItemClicked(userAvatar, userId, uuid_Value, username);
                }
            });
        }
    }

    public void addAddSearchResults(AddSearchResult addSearchResult) {
        addSearchResults.add(addSearchResult);
        notifyItemInserted(addSearchResults.size() - 1);
    }

    public void removeAddSearchResultAtEnd(int index){
        if (index >= addSearchResults.size()) return;
        addSearchResults.remove(addSearchResults.size() - (1 + index));
    }

    public void clearAddSearchResults(){
        addSearchResults.clear();
        notifyDataSetChanged();
    }
}
